package de.rj.urwalking.loader;

import java.util.ArrayList;

import android.content.Context;

import com.google.android.gms.maps.model.LatLng;

import de.ralf.urwalking.soplugin.EPSGPoint;
import de.rj.urwalking.data.Point;
import de.rj.urwalking.db.ReadWritePostgreSQL;
import de.rj.urwalking.util.GeoUtil;
import de.rj.urwalking.util.PrefUtil;

/**
 * 
 * 
 * @author rjj
 * 
 */

public class Check3dDataLoader extends AsyncLoader<Boolean> {

	
	private Context context;

	/** EPSG31468-Hochwert */
	private double northing;
	/** EPSG31468-Rechtswert */
	private double easting;

	

	public Check3dDataLoader(Context context, LatLng location) {
		super(context);
		this.context = context;		

		// convert GPS coordinates to EPSG 31468, which is used in the database
		EPSGPoint epsgPoint = GeoUtil.convertLatLonToEPSG31468(location.longitude, location.latitude);
		northing = epsgPoint.getY();
		easting = epsgPoint.getX();

	}

	@Override
	public Boolean loadInBackground() {

		// connect with db
		ReadWritePostgreSQL dbWrapper = new ReadWritePostgreSQL();

		double radius = PrefUtil.readRadius(context);

		// get points for outlines of buildings
		ArrayList<Point> points = dbWrapper.getPointsWithinRadius(easting, northing, radius);

		dbWrapper.closeConnection();


		return (points.size() > 0) ? true : false;
	}

	@Override
	protected void onStartLoading() {

		forceLoad();

	}
	
}
